

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="Description" content="scikit-learn: machine learning in Python">

  
  <title>1.10. Decision Trees &mdash; scikit-learn 0.22 documentation</title>
  
  <link rel="canonical" href="http://scikit-learn.org/stable/modules/tree.html" />

  
  <link rel="shortcut icon" href="../_static/favicon.ico"/>
  

  <link rel="stylesheet" href="../_static/css/vendor/bootstrap.min.css" type="text/css" />
  <link rel="stylesheet" href="../_static/gallery.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
<script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script> 
</head>
<body>
<nav id="navbar" class="sk-docs-navbar navbar navbar-expand-md navbar-light bg-light py-0">
  <div class="container-fluid sk-docs-container px-0">
      <a class="navbar-brand py-0" href="../index.html">
        <img
          class="sk-brand-img"
          src="../_static/scikit-learn-logo-small.png"
          alt="logo"/>
      </a>
    <button
      id="sk-navbar-toggler"
      class="navbar-toggler"
      type="button"
      data-toggle="collapse"
      data-target="#navbarSupportedContent"
      aria-controls="navbarSupportedContent"
      aria-expanded="false"
      aria-label="Toggle navigation"
    >
      <span class="navbar-toggler-icon"></span>
    </button>

    <div class="sk-navbar-collapse collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav mr-auto">
        <li class="nav-item">
          <a class="sk-nav-link nav-link" href="../install.html">Install</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link" href="../user_guide.html">User Guide</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link" href="classes.html">API</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link" href="../auto_examples/index.html">Examples</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link nav-more-item-mobile-items" href="../getting_started.html">Getting Started</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link nav-more-item-mobile-items" href="../tutorial/index.html">Tutorial</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link nav-more-item-mobile-items" href="../glossary.html">Glossary</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link nav-more-item-mobile-items" href="../developers/index.html">Development</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link nav-more-item-mobile-items" href="../faq.html">FAQ</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link nav-more-item-mobile-items" href="../related_projects.html">Related packages</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link nav-more-item-mobile-items" href="../roadmap.html">Roadmap</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link nav-more-item-mobile-items" href="../about.html">About us</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link nav-more-item-mobile-items" href="https://github.com/scikit-learn/scikit-learn">GitHub</a>
        </li>
        <li class="nav-item">
          <a class="sk-nav-link nav-link nav-more-item-mobile-items" href="https://scikit-learn.org/dev/versions.html">Other Versions</a>
        </li>
        <li class="nav-item dropdown nav-more-item-dropdown">
          <a class="sk-nav-link nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">More</a>
          <div class="dropdown-menu" aria-labelledby="navbarDropdown">
              <a class="sk-nav-dropdown-item dropdown-item" href="../getting_started.html">Getting Started</a>
              <a class="sk-nav-dropdown-item dropdown-item" href="../tutorial/index.html">Tutorial</a>
              <a class="sk-nav-dropdown-item dropdown-item" href="../glossary.html">Glossary</a>
              <a class="sk-nav-dropdown-item dropdown-item" href="../developers/index.html">Development</a>
              <a class="sk-nav-dropdown-item dropdown-item" href="../faq.html">FAQ</a>
              <a class="sk-nav-dropdown-item dropdown-item" href="../related_projects.html">Related packages</a>
              <a class="sk-nav-dropdown-item dropdown-item" href="../roadmap.html">Roadmap</a>
              <a class="sk-nav-dropdown-item dropdown-item" href="../about.html">About us</a>
              <a class="sk-nav-dropdown-item dropdown-item" href="https://github.com/scikit-learn/scikit-learn">GitHub</a>
              <a class="sk-nav-dropdown-item dropdown-item" href="https://scikit-learn.org/dev/versions.html">Other Versions</a>
          </div>
        </li>
      </ul>
      <div id="searchbox" role="search">
          <div class="searchformwrapper">
          <form class="search" action="../search.html" method="get">
            <input class="sk-search-text-input" type="text" name="q" aria-labelledby="searchlabel" />
            <input class="sk-search-text-btn" type="submit" value="Go" />
          </form>
          </div>
      </div>
    </div>
  </div>
</nav>
<div class="d-flex" id="sk-doc-wrapper">
    <input type="checkbox" name="sk-toggle-checkbox" id="sk-toggle-checkbox">
    <label id="sk-sidemenu-toggle" class="sk-btn-toggle-toc btn sk-btn-primary" for="sk-toggle-checkbox">Toggle Menu</label>
    <div id="sk-sidebar-wrapper" class="border-right">
      <div class="sk-sidebar-toc-wrapper">
        <div class="sk-sidebar-toc-logo">
          <a href="../index.html">
            <img
              class="sk-brand-img"
              src="../_static/scikit-learn-logo-small.png"
              alt="logo"/>
          </a>
        </div>
        <div class="btn-group w-100 mb-2" role="group" aria-label="rellinks">
            <a href="naive_bayes.html" role="button" class="btn sk-btn-rellink py-1" sk-rellink-tooltip="1.9. Naive Bayes">Prev</a><a href="../supervised_learning.html" role="button" class="btn sk-btn-rellink py-1" sk-rellink-tooltip="1. Supervised learning">Up</a>
            <a href="ensemble.html" role="button" class="btn sk-btn-rellink py-1" sk-rellink-tooltip="1.11. Ensemble methods">Next</a>
        </div>
        <div class="alert alert-danger p-1 mb-2" role="alert">
          <p class="text-center mb-0">
          <strong>scikit-learn 0.22</strong><br/>
          <a href="http://scikit-learn.org/dev/versions.html">Other versions</a>
          </p>
        </div>
        <div class="alert alert-warning p-1 mb-2" role="alert">
          <p class="text-center mb-0">
            Please <a class="font-weight-bold" href="../about.html#citing-scikit-learn"><string>cite us</string></a> if you use the software.
          </p>
        </div>
          <div class="sk-sidebar-toc">
            <ul>
<li><a class="reference internal" href="#">1.10. Decision Trees</a><ul>
<li><a class="reference internal" href="#classification">1.10.1. Classification</a></li>
<li><a class="reference internal" href="#regression">1.10.2. Regression</a></li>
<li><a class="reference internal" href="#multi-output-problems">1.10.3. Multi-output problems</a></li>
<li><a class="reference internal" href="#complexity">1.10.4. Complexity</a></li>
<li><a class="reference internal" href="#tips-on-practical-use">1.10.5. Tips on practical use</a></li>
<li><a class="reference internal" href="#tree-algorithms-id3-c4-5-c5-0-and-cart">1.10.6. Tree algorithms: ID3, C4.5, C5.0 and CART</a></li>
<li><a class="reference internal" href="#mathematical-formulation">1.10.7. Mathematical formulation</a><ul>
<li><a class="reference internal" href="#classification-criteria">1.10.7.1. Classification criteria</a></li>
<li><a class="reference internal" href="#regression-criteria">1.10.7.2. Regression criteria</a></li>
</ul>
</li>
<li><a class="reference internal" href="#minimal-cost-complexity-pruning">1.10.8. Minimal Cost-Complexity Pruning</a></li>
</ul>
</li>
</ul>

          </div>
      </div>
    </div>
    <div id="sk-page-content-wrapper">
      <div class="sk-page-content container-fluid body px-md-3" role="main">
        
  <div class="section" id="decision-trees">
<span id="tree"></span><h1>1.10. Decision Trees<a class="headerlink" href="#decision-trees" title="Permalink to this headline">¶</a></h1>
<p><strong>Decision Trees (DTs)</strong> are a non-parametric supervised learning method used
for <a class="reference internal" href="#tree-classification"><span class="std std-ref">classification</span></a> and <a class="reference internal" href="#tree-regression"><span class="std std-ref">regression</span></a>. The goal is to create a model that predicts the value of a
target variable by learning simple decision rules inferred from the data
features.</p>
<p>For instance, in the example below, decision trees learn from data to
approximate a sine curve with a set of if-then-else decision rules. The deeper
the tree, the more complex the decision rules and the fitter the model.</p>
<div class="figure align-center">
<a class="reference external image-reference" href="../auto_examples/tree/plot_tree_regression.html"><img alt="modules/../auto_examples/tree/images/sphx_glr_plot_tree_regression_001.png" src="modules/../auto_examples/tree/images/sphx_glr_plot_tree_regression_001.png" /></a>
</div>
<p>Some advantages of decision trees are:</p>
<blockquote>
<div><ul class="simple">
<li><p>Simple to understand and to interpret. Trees can be visualised.</p></li>
<li><p>Requires little data preparation. Other techniques often require data
normalisation, dummy variables need to be created and blank values to
be removed. Note however that this module does not support missing
values.</p></li>
<li><p>The cost of using the tree (i.e., predicting data) is logarithmic in the
number of data points used to train the tree.</p></li>
<li><p>Able to handle both numerical and categorical data. Other techniques
are usually specialised in analysing datasets that have only one type
of variable. See <a class="reference internal" href="#tree-algorithms"><span class="std std-ref">algorithms</span></a> for more
information.</p></li>
<li><p>Able to handle multi-output problems.</p></li>
<li><p>Uses a white box model. If a given situation is observable in a model,
the explanation for the condition is easily explained by boolean logic.
By contrast, in a black box model (e.g., in an artificial neural
network), results may be more difficult to interpret.</p></li>
<li><p>Possible to validate a model using statistical tests. That makes it
possible to account for the reliability of the model.</p></li>
<li><p>Performs well even if its assumptions are somewhat violated by
the true model from which the data were generated.</p></li>
</ul>
</div></blockquote>
<p>The disadvantages of decision trees include:</p>
<blockquote>
<div><ul class="simple">
<li><p>Decision-tree learners can create over-complex trees that do not
generalise the data well. This is called overfitting. Mechanisms
such as pruning (not currently supported), setting the minimum
number of samples required at a leaf node or setting the maximum
depth of the tree are necessary to avoid this problem.</p></li>
<li><p>Decision trees can be unstable because small variations in the
data might result in a completely different tree being generated.
This problem is mitigated by using decision trees within an
ensemble.</p></li>
<li><p>The problem of learning an optimal decision tree is known to be
NP-complete under several aspects of optimality and even for simple
concepts. Consequently, practical decision-tree learning algorithms
are based on heuristic algorithms such as the greedy algorithm where
locally optimal decisions are made at each node. Such algorithms
cannot guarantee to return the globally optimal decision tree.  This
can be mitigated by training multiple trees in an ensemble learner,
where the features and samples are randomly sampled with replacement.</p></li>
<li><p>There are concepts that are hard to learn because decision trees
do not express them easily, such as XOR, parity or multiplexer problems.</p></li>
<li><p>Decision tree learners create biased trees if some classes dominate.
It is therefore recommended to balance the dataset prior to fitting
with the decision tree.</p></li>
</ul>
</div></blockquote>
<div class="section" id="classification">
<span id="tree-classification"></span><h2>1.10.1. Classification<a class="headerlink" href="#classification" title="Permalink to this headline">¶</a></h2>
<p><a class="reference internal" href="generated/sklearn.tree.DecisionTreeClassifier.html#sklearn.tree.DecisionTreeClassifier" title="sklearn.tree.DecisionTreeClassifier"><code class="xref py py-class docutils literal notranslate"><span class="pre">DecisionTreeClassifier</span></code></a> is a class capable of performing multi-class
classification on a dataset.</p>
<p>As with other classifiers, <a class="reference internal" href="generated/sklearn.tree.DecisionTreeClassifier.html#sklearn.tree.DecisionTreeClassifier" title="sklearn.tree.DecisionTreeClassifier"><code class="xref py py-class docutils literal notranslate"><span class="pre">DecisionTreeClassifier</span></code></a> takes as input two arrays:
an array X, sparse or dense, of size <code class="docutils literal notranslate"><span class="pre">[n_samples,</span> <span class="pre">n_features]</span></code>  holding the
training samples, and an array Y of integer values, size <code class="docutils literal notranslate"><span class="pre">[n_samples]</span></code>,
holding the class labels for the training samples:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sklearn</span> <span class="kn">import</span> <span class="n">tree</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">X</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">]]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Y</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">clf</span> <span class="o">=</span> <span class="n">tree</span><span class="o">.</span><span class="n">DecisionTreeClassifier</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">clf</span> <span class="o">=</span> <span class="n">clf</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">Y</span><span class="p">)</span>
</pre></div>
</div>
<p>After being fitted, the model can then be used to predict the class of samples:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">clf</span><span class="o">.</span><span class="n">predict</span><span class="p">([[</span><span class="mf">2.</span><span class="p">,</span> <span class="mf">2.</span><span class="p">]])</span>
<span class="go">array([1])</span>
</pre></div>
</div>
<p>Alternatively, the probability of each class can be predicted, which is the
fraction of training samples of the same class in a leaf:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">clf</span><span class="o">.</span><span class="n">predict_proba</span><span class="p">([[</span><span class="mf">2.</span><span class="p">,</span> <span class="mf">2.</span><span class="p">]])</span>
<span class="go">array([[0., 1.]])</span>
</pre></div>
</div>
<p><a class="reference internal" href="generated/sklearn.tree.DecisionTreeClassifier.html#sklearn.tree.DecisionTreeClassifier" title="sklearn.tree.DecisionTreeClassifier"><code class="xref py py-class docutils literal notranslate"><span class="pre">DecisionTreeClassifier</span></code></a> is capable of both binary (where the
labels are [-1, 1]) classification and multiclass (where the labels are
[0, …, K-1]) classification.</p>
<p>Using the Iris dataset, we can construct a tree as follows:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sklearn.datasets</span> <span class="kn">import</span> <span class="n">load_iris</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sklearn</span> <span class="kn">import</span> <span class="n">tree</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">X</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">load_iris</span><span class="p">(</span><span class="n">return_X_y</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">clf</span> <span class="o">=</span> <span class="n">tree</span><span class="o">.</span><span class="n">DecisionTreeClassifier</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">clf</span> <span class="o">=</span> <span class="n">clf</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
</pre></div>
</div>
<p>Once trained, you can plot the tree with the plot_tree function:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">tree</span><span class="o">.</span><span class="n">plot_tree</span><span class="p">(</span><span class="n">clf</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">iris</span><span class="o">.</span><span class="n">data</span><span class="p">,</span> <span class="n">iris</span><span class="o">.</span><span class="n">target</span><span class="p">))</span> 
</pre></div>
</div>
<div class="figure align-center">
<a class="reference external image-reference" href="../auto_examples/tree/plot_iris_dtc.html"><img alt="modules/../auto_examples/tree/images/sphx_glr_plot_iris_dtc_002.png" src="modules/../auto_examples/tree/images/sphx_glr_plot_iris_dtc_002.png" /></a>
</div>
<p>We can also export the tree in <a class="reference external" href="https://www.graphviz.org/">Graphviz</a> format using the <a class="reference internal" href="generated/sklearn.tree.export_graphviz.html#sklearn.tree.export_graphviz" title="sklearn.tree.export_graphviz"><code class="xref py py-func docutils literal notranslate"><span class="pre">export_graphviz</span></code></a>
exporter. If you use the <a class="reference external" href="https://conda.io">conda</a> package manager, the graphviz binaries</p>
<p>and the python package can be installed with</p>
<blockquote>
<div><p>conda install python-graphviz</p>
</div></blockquote>
<p>Alternatively binaries for graphviz can be downloaded from the graphviz project homepage,
and the Python wrapper installed from pypi with <code class="docutils literal notranslate"><span class="pre">pip</span> <span class="pre">install</span> <span class="pre">graphviz</span></code>.</p>
<p>Below is an example graphviz export of the above tree trained on the entire
iris dataset; the results are saved in an output file <code class="docutils literal notranslate"><span class="pre">iris.pdf</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">graphviz</span> 
<span class="gp">&gt;&gt;&gt; </span><span class="n">dot_data</span> <span class="o">=</span> <span class="n">tree</span><span class="o">.</span><span class="n">export_graphviz</span><span class="p">(</span><span class="n">clf</span><span class="p">,</span> <span class="n">out_file</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span> 
<span class="gp">&gt;&gt;&gt; </span><span class="n">graph</span> <span class="o">=</span> <span class="n">graphviz</span><span class="o">.</span><span class="n">Source</span><span class="p">(</span><span class="n">dot_data</span><span class="p">)</span> 
<span class="gp">&gt;&gt;&gt; </span><span class="n">graph</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="s2">&quot;iris&quot;</span><span class="p">)</span> 
</pre></div>
</div>
<p>The <a class="reference internal" href="generated/sklearn.tree.export_graphviz.html#sklearn.tree.export_graphviz" title="sklearn.tree.export_graphviz"><code class="xref py py-func docutils literal notranslate"><span class="pre">export_graphviz</span></code></a> exporter also supports a variety of aesthetic
options, including coloring nodes by their class (or value for regression) and
using explicit variable and class names if desired. Jupyter notebooks also
render these plots inline automatically:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">dot_data</span> <span class="o">=</span> <span class="n">tree</span><span class="o">.</span><span class="n">export_graphviz</span><span class="p">(</span><span class="n">clf</span><span class="p">,</span> <span class="n">out_file</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> 
<span class="gp">... </span>                     <span class="n">feature_names</span><span class="o">=</span><span class="n">iris</span><span class="o">.</span><span class="n">feature_names</span><span class="p">,</span>  
<span class="gp">... </span>                     <span class="n">class_names</span><span class="o">=</span><span class="n">iris</span><span class="o">.</span><span class="n">target_names</span><span class="p">,</span>  
<span class="gp">... </span>                     <span class="n">filled</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">rounded</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>  
<span class="gp">... </span>                     <span class="n">special_characters</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>  
<span class="gp">&gt;&gt;&gt; </span><span class="n">graph</span> <span class="o">=</span> <span class="n">graphviz</span><span class="o">.</span><span class="n">Source</span><span class="p">(</span><span class="n">dot_data</span><span class="p">)</span>  
<span class="gp">&gt;&gt;&gt; </span><span class="n">graph</span> 
</pre></div>
</div>
<div class="figure align-center">
<img alt="../_images/iris.svg" src="../_images/iris.svg" /></div>
<div class="figure align-center">
<a class="reference external image-reference" href="../auto_examples/tree/plot_iris_dtc.html"><img alt="modules/../auto_examples/tree/images/sphx_glr_plot_iris_dtc_001.png" src="modules/../auto_examples/tree/images/sphx_glr_plot_iris_dtc_001.png" /></a>
</div>
<p>Alternatively, the tree can also be exported in textual format with the
function <a class="reference internal" href="generated/sklearn.tree.export_text.html#sklearn.tree.export_text" title="sklearn.tree.export_text"><code class="xref py py-func docutils literal notranslate"><span class="pre">export_text</span></code></a>. This method doesn’t require the installation
of external libraries and is more compact:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sklearn.datasets</span> <span class="kn">import</span> <span class="n">load_iris</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sklearn.tree</span> <span class="kn">import</span> <span class="n">DecisionTreeClassifier</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sklearn.tree.export</span> <span class="kn">import</span> <span class="n">export_text</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">iris</span> <span class="o">=</span> <span class="n">load_iris</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">decision_tree</span> <span class="o">=</span> <span class="n">DecisionTreeClassifier</span><span class="p">(</span><span class="n">random_state</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">max_depth</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">decision_tree</span> <span class="o">=</span> <span class="n">decision_tree</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">iris</span><span class="o">.</span><span class="n">data</span><span class="p">,</span> <span class="n">iris</span><span class="o">.</span><span class="n">target</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">export_text</span><span class="p">(</span><span class="n">decision_tree</span><span class="p">,</span> <span class="n">feature_names</span><span class="o">=</span><span class="n">iris</span><span class="p">[</span><span class="s1">&#39;feature_names&#39;</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">r</span><span class="p">)</span>
<span class="go">|--- petal width (cm) &lt;= 0.80</span>
<span class="go">|   |--- class: 0</span>
<span class="go">|--- petal width (cm) &gt;  0.80</span>
<span class="go">|   |--- petal width (cm) &lt;= 1.75</span>
<span class="go">|   |   |--- class: 1</span>
<span class="go">|   |--- petal width (cm) &gt;  1.75</span>
<span class="go">|   |   |--- class: 2</span>
<span class="go">&lt;BLANKLINE&gt;</span>
</pre></div>
</div>
<div class="topic">
<p class="topic-title">Examples:</p>
<ul class="simple">
<li><p><a class="reference internal" href="../auto_examples/tree/plot_iris_dtc.html#sphx-glr-auto-examples-tree-plot-iris-dtc-py"><span class="std std-ref">Plot the decision surface of a decision tree on the iris dataset</span></a></p></li>
<li><p><a class="reference internal" href="../auto_examples/tree/plot_unveil_tree_structure.html#sphx-glr-auto-examples-tree-plot-unveil-tree-structure-py"><span class="std std-ref">Understanding the decision tree structure</span></a></p></li>
</ul>
</div>
</div>
<div class="section" id="regression">
<span id="tree-regression"></span><h2>1.10.2. Regression<a class="headerlink" href="#regression" title="Permalink to this headline">¶</a></h2>
<div class="figure align-center">
<a class="reference external image-reference" href="../auto_examples/tree/plot_tree_regression.html"><img alt="modules/../auto_examples/tree/images/sphx_glr_plot_tree_regression_001.png" src="modules/../auto_examples/tree/images/sphx_glr_plot_tree_regression_001.png" /></a>
</div>
<p>Decision trees can also be applied to regression problems, using the
<a class="reference internal" href="generated/sklearn.tree.DecisionTreeRegressor.html#sklearn.tree.DecisionTreeRegressor" title="sklearn.tree.DecisionTreeRegressor"><code class="xref py py-class docutils literal notranslate"><span class="pre">DecisionTreeRegressor</span></code></a> class.</p>
<p>As in the classification setting, the fit method will take as argument arrays X
and y, only that in this case y is expected to have floating point values
instead of integer values:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sklearn</span> <span class="kn">import</span> <span class="n">tree</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">X</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">]]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">y</span> <span class="o">=</span> <span class="p">[</span><span class="mf">0.5</span><span class="p">,</span> <span class="mf">2.5</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">clf</span> <span class="o">=</span> <span class="n">tree</span><span class="o">.</span><span class="n">DecisionTreeRegressor</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">clf</span> <span class="o">=</span> <span class="n">clf</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">clf</span><span class="o">.</span><span class="n">predict</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">]])</span>
<span class="go">array([0.5])</span>
</pre></div>
</div>
<div class="topic">
<p class="topic-title">Examples:</p>
<ul class="simple">
<li><p><a class="reference internal" href="../auto_examples/tree/plot_tree_regression.html#sphx-glr-auto-examples-tree-plot-tree-regression-py"><span class="std std-ref">Decision Tree Regression</span></a></p></li>
</ul>
</div>
</div>
<div class="section" id="multi-output-problems">
<span id="tree-multioutput"></span><h2>1.10.3. Multi-output problems<a class="headerlink" href="#multi-output-problems" title="Permalink to this headline">¶</a></h2>
<p>A multi-output problem is a supervised learning problem with several outputs
to predict, that is when Y is a 2d array of size <code class="docutils literal notranslate"><span class="pre">[n_samples,</span> <span class="pre">n_outputs]</span></code>.</p>
<p>When there is no correlation between the outputs, a very simple way to solve
this kind of problem is to build n independent models, i.e. one for each
output, and then to use those models to independently predict each one of the n
outputs. However, because it is likely that the output values related to the
same input are themselves correlated, an often better way is to build a single
model capable of predicting simultaneously all n outputs. First, it requires
lower training time since only a single estimator is built. Second, the
generalization accuracy of the resulting estimator may often be increased.</p>
<p>With regard to decision trees, this strategy can readily be used to support
multi-output problems. This requires the following changes:</p>
<blockquote>
<div><ul class="simple">
<li><p>Store n output values in leaves, instead of 1;</p></li>
<li><p>Use splitting criteria that compute the average reduction across all
n outputs.</p></li>
</ul>
</div></blockquote>
<p>This module offers support for multi-output problems by implementing this
strategy in both <a class="reference internal" href="generated/sklearn.tree.DecisionTreeClassifier.html#sklearn.tree.DecisionTreeClassifier" title="sklearn.tree.DecisionTreeClassifier"><code class="xref py py-class docutils literal notranslate"><span class="pre">DecisionTreeClassifier</span></code></a> and
<a class="reference internal" href="generated/sklearn.tree.DecisionTreeRegressor.html#sklearn.tree.DecisionTreeRegressor" title="sklearn.tree.DecisionTreeRegressor"><code class="xref py py-class docutils literal notranslate"><span class="pre">DecisionTreeRegressor</span></code></a>. If a decision tree is fit on an output array Y
of size <code class="docutils literal notranslate"><span class="pre">[n_samples,</span> <span class="pre">n_outputs]</span></code> then the resulting estimator will:</p>
<blockquote>
<div><ul class="simple">
<li><p>Output n_output values upon <code class="docutils literal notranslate"><span class="pre">predict</span></code>;</p></li>
<li><p>Output a list of n_output arrays of class probabilities upon
<code class="docutils literal notranslate"><span class="pre">predict_proba</span></code>.</p></li>
</ul>
</div></blockquote>
<p>The use of multi-output trees for regression is demonstrated in
<a class="reference internal" href="../auto_examples/tree/plot_tree_regression_multioutput.html#sphx-glr-auto-examples-tree-plot-tree-regression-multioutput-py"><span class="std std-ref">Multi-output Decision Tree Regression</span></a>. In this example, the input
X is a single real value and the outputs Y are the sine and cosine of X.</p>
<div class="figure align-center">
<a class="reference external image-reference" href="../auto_examples/tree/plot_tree_regression_multioutput.html"><img alt="modules/../auto_examples/tree/images/sphx_glr_plot_tree_regression_multioutput_001.png" src="modules/../auto_examples/tree/images/sphx_glr_plot_tree_regression_multioutput_001.png" /></a>
</div>
<p>The use of multi-output trees for classification is demonstrated in
<a class="reference internal" href="../auto_examples/plot_multioutput_face_completion.html#sphx-glr-auto-examples-plot-multioutput-face-completion-py"><span class="std std-ref">Face completion with a multi-output estimators</span></a>. In this example, the inputs
X are the pixels of the upper half of faces and the outputs Y are the pixels of
the lower half of those faces.</p>
<div class="figure align-center">
<a class="reference external image-reference" href="../auto_examples/plot_multioutput_face_completion.html"><img alt="modules/../auto_examples/images/sphx_glr_plot_multioutput_face_completion_001.png" src="modules/../auto_examples/images/sphx_glr_plot_multioutput_face_completion_001.png" /></a>
</div>
<div class="topic">
<p class="topic-title">Examples:</p>
<ul class="simple">
<li><p><a class="reference internal" href="../auto_examples/tree/plot_tree_regression_multioutput.html#sphx-glr-auto-examples-tree-plot-tree-regression-multioutput-py"><span class="std std-ref">Multi-output Decision Tree Regression</span></a></p></li>
<li><p><a class="reference internal" href="../auto_examples/plot_multioutput_face_completion.html#sphx-glr-auto-examples-plot-multioutput-face-completion-py"><span class="std std-ref">Face completion with a multi-output estimators</span></a></p></li>
</ul>
</div>
<div class="topic">
<p class="topic-title">References:</p>
<ul class="simple">
<li><p>M. Dumont et al,  <a class="reference external" href="http://www.montefiore.ulg.ac.be/services/stochastic/pubs/2009/DMWG09/dumont-visapp09-shortpaper.pdf">Fast multi-class image annotation with random subwindows
and multiple output randomized trees</a>, International Conference on
Computer Vision Theory and Applications 2009</p></li>
</ul>
</div>
</div>
<div class="section" id="complexity">
<span id="tree-complexity"></span><h2>1.10.4. Complexity<a class="headerlink" href="#complexity" title="Permalink to this headline">¶</a></h2>
<p>In general, the run time cost to construct a balanced binary tree is
<span class="math notranslate nohighlight">\(O(n_{samples}n_{features}\log(n_{samples}))\)</span> and query time
<span class="math notranslate nohighlight">\(O(\log(n_{samples}))\)</span>.  Although the tree construction algorithm attempts
to generate balanced trees, they will not always be balanced.  Assuming that the
subtrees remain approximately balanced, the cost at each node consists of
searching through <span class="math notranslate nohighlight">\(O(n_{features})\)</span> to find the feature that offers the
largest reduction in entropy.  This has a cost of
<span class="math notranslate nohighlight">\(O(n_{features}n_{samples}\log(n_{samples}))\)</span> at each node, leading to a
total cost over the entire trees (by summing the cost at each node) of
<span class="math notranslate nohighlight">\(O(n_{features}n_{samples}^{2}\log(n_{samples}))\)</span>.</p>
</div>
<div class="section" id="tips-on-practical-use">
<h2>1.10.5. Tips on practical use<a class="headerlink" href="#tips-on-practical-use" title="Permalink to this headline">¶</a></h2>
<blockquote>
<div><ul class="simple">
<li><p>Decision trees tend to overfit on data with a large number of features.
Getting the right ratio of samples to number of features is important, since
a tree with few samples in high dimensional space is very likely to overfit.</p></li>
<li><p>Consider performing  dimensionality reduction (<a class="reference internal" href="decomposition.html#pca"><span class="std std-ref">PCA</span></a>,
<a class="reference internal" href="decomposition.html#ica"><span class="std std-ref">ICA</span></a>, or <a class="reference internal" href="feature_selection.html#feature-selection"><span class="std std-ref">Feature selection</span></a>) beforehand to
give your tree a better chance of finding features that are discriminative.</p></li>
<li><p><a class="reference internal" href="../auto_examples/tree/plot_unveil_tree_structure.html#sphx-glr-auto-examples-tree-plot-unveil-tree-structure-py"><span class="std std-ref">Understanding the decision tree structure</span></a> will help
in gaining more insights about how the decision tree makes predictions, which is
important for understanding the important features in the data.</p></li>
<li><p>Visualise your tree as you are training by using the <code class="docutils literal notranslate"><span class="pre">export</span></code>
function.  Use <code class="docutils literal notranslate"><span class="pre">max_depth=3</span></code> as an initial tree depth to get a feel for
how the tree is fitting to your data, and then increase the depth.</p></li>
<li><p>Remember that the number of samples required to populate the tree doubles
for each additional level the tree grows to.  Use <code class="docutils literal notranslate"><span class="pre">max_depth</span></code> to control
the size of the tree to prevent overfitting.</p></li>
<li><p>Use <code class="docutils literal notranslate"><span class="pre">min_samples_split</span></code> or <code class="docutils literal notranslate"><span class="pre">min_samples_leaf</span></code> to ensure that multiple
samples inform every decision in the tree, by controlling which splits will
be considered. A very small number will usually mean the tree will overfit,
whereas a large number will prevent the tree from learning the data. Try
<code class="docutils literal notranslate"><span class="pre">min_samples_leaf=5</span></code> as an initial value. If the sample size varies
greatly, a float number can be used as percentage in these two parameters.
While <code class="docutils literal notranslate"><span class="pre">min_samples_split</span></code> can create arbitrarily small leaves,
<code class="docutils literal notranslate"><span class="pre">min_samples_leaf</span></code> guarantees that each leaf has a minimum size, avoiding
low-variance, over-fit leaf nodes in regression problems.  For
classification with few classes, <code class="docutils literal notranslate"><span class="pre">min_samples_leaf=1</span></code> is often the best
choice.</p></li>
<li><p>Balance your dataset before training to prevent the tree from being biased
toward the classes that are dominant. Class balancing can be done by
sampling an equal number of samples from each class, or preferably by
normalizing the sum of the sample weights (<code class="docutils literal notranslate"><span class="pre">sample_weight</span></code>) for each
class to the same value. Also note that weight-based pre-pruning criteria,
such as <code class="docutils literal notranslate"><span class="pre">min_weight_fraction_leaf</span></code>, will then be less biased toward
dominant classes than criteria that are not aware of the sample weights,
like <code class="docutils literal notranslate"><span class="pre">min_samples_leaf</span></code>.</p></li>
<li><p>If the samples are weighted, it will be easier to optimize the tree
structure using weight-based pre-pruning criterion such as
<code class="docutils literal notranslate"><span class="pre">min_weight_fraction_leaf</span></code>, which ensure that leaf nodes contain at least
a fraction of the overall sum of the sample weights.</p></li>
<li><p>All decision trees use <code class="docutils literal notranslate"><span class="pre">np.float32</span></code> arrays internally.
If training data is not in this format, a copy of the dataset will be made.</p></li>
<li><p>If the input matrix X is very sparse, it is recommended to convert to sparse
<code class="docutils literal notranslate"><span class="pre">csc_matrix</span></code> before calling fit and sparse <code class="docutils literal notranslate"><span class="pre">csr_matrix</span></code> before calling
predict. Training time can be orders of magnitude faster for a sparse
matrix input compared to a dense matrix when features have zero values in
most of the samples.</p></li>
</ul>
</div></blockquote>
</div>
<div class="section" id="tree-algorithms-id3-c4-5-c5-0-and-cart">
<span id="tree-algorithms"></span><h2>1.10.6. Tree algorithms: ID3, C4.5, C5.0 and CART<a class="headerlink" href="#tree-algorithms-id3-c4-5-c5-0-and-cart" title="Permalink to this headline">¶</a></h2>
<p>What are all the various decision tree algorithms and how do they differ
from each other? Which one is implemented in scikit-learn?</p>
<p><a class="reference external" href="https://en.wikipedia.org/wiki/ID3_algorithm">ID3</a> (Iterative Dichotomiser 3) was developed in 1986 by Ross Quinlan.
The algorithm creates a multiway tree, finding for each node (i.e. in
a greedy manner) the categorical feature that will yield the largest
information gain for categorical targets. Trees are grown to their
maximum size and then a pruning step is usually applied to improve the
ability of the tree to generalise to unseen data.</p>
<p>C4.5 is the successor to ID3 and removed the restriction that features
must be categorical by dynamically defining a discrete attribute (based
on numerical variables) that partitions the continuous attribute value
into a discrete set of intervals. C4.5 converts the trained trees
(i.e. the output of the ID3 algorithm) into sets of if-then rules.
These accuracy of each rule is then evaluated to determine the order
in which they should be applied. Pruning is done by removing a rule’s
precondition if the accuracy of the rule improves without it.</p>
<p>C5.0 is Quinlan’s latest version release under a proprietary license.
It uses less memory and builds smaller rulesets than C4.5 while being
more accurate.</p>
<p><a class="reference external" href="https://en.wikipedia.org/wiki/Predictive_analytics#Classification_and_regression_trees_.28CART.29">CART</a> (Classification and Regression Trees) is very similar to C4.5, but
it differs in that it supports numerical target variables (regression) and
does not compute rule sets. CART constructs binary trees using the feature
and threshold that yield the largest information gain at each node.</p>
<p>scikit-learn uses an optimised version of the CART algorithm; however, scikit-learn
implementation does not support categorical variables for now.</p>
</div>
<div class="section" id="mathematical-formulation">
<span id="tree-mathematical-formulation"></span><h2>1.10.7. Mathematical formulation<a class="headerlink" href="#mathematical-formulation" title="Permalink to this headline">¶</a></h2>
<p>Given training vectors <span class="math notranslate nohighlight">\(x_i \in R^n\)</span>, i=1,…, l and a label vector
<span class="math notranslate nohighlight">\(y \in R^l\)</span>, a decision tree recursively partitions the space such
that the samples with the same labels are grouped together.</p>
<p>Let the data at node <span class="math notranslate nohighlight">\(m\)</span> be represented by <span class="math notranslate nohighlight">\(Q\)</span>. For
each candidate split <span class="math notranslate nohighlight">\(\theta = (j, t_m)\)</span> consisting of a
feature <span class="math notranslate nohighlight">\(j\)</span> and threshold <span class="math notranslate nohighlight">\(t_m\)</span>, partition the data into
<span class="math notranslate nohighlight">\(Q_{left}(\theta)\)</span> and <span class="math notranslate nohighlight">\(Q_{right}(\theta)\)</span> subsets</p>
<div class="math notranslate nohighlight">
\[ \begin{align}\begin{aligned}Q_{left}(\theta) = {(x, y) | x_j &lt;= t_m}\\Q_{right}(\theta) = Q \setminus Q_{left}(\theta)\end{aligned}\end{align} \]</div>
<p>The impurity at <span class="math notranslate nohighlight">\(m\)</span> is computed using an impurity function
<span class="math notranslate nohighlight">\(H()\)</span>, the choice of which depends on the task being solved
(classification or regression)</p>
<div class="math notranslate nohighlight">
\[G(Q, \theta) = \frac{n_{left}}{N_m} H(Q_{left}(\theta))
+ \frac{n_{right}}{N_m} H(Q_{right}(\theta))\]</div>
<p>Select the parameters that minimises the impurity</p>
<div class="math notranslate nohighlight">
\[\theta^* = \operatorname{argmin}_\theta  G(Q, \theta)\]</div>
<p>Recurse for subsets <span class="math notranslate nohighlight">\(Q_{left}(\theta^*)\)</span> and
<span class="math notranslate nohighlight">\(Q_{right}(\theta^*)\)</span> until the maximum allowable depth is reached,
<span class="math notranslate nohighlight">\(N_m &lt; \min_{samples}\)</span> or <span class="math notranslate nohighlight">\(N_m = 1\)</span>.</p>
<div class="section" id="classification-criteria">
<h3>1.10.7.1. Classification criteria<a class="headerlink" href="#classification-criteria" title="Permalink to this headline">¶</a></h3>
<p>If a target is a classification outcome taking on values 0,1,…,K-1,
for node <span class="math notranslate nohighlight">\(m\)</span>, representing a region <span class="math notranslate nohighlight">\(R_m\)</span> with <span class="math notranslate nohighlight">\(N_m\)</span>
observations, let</p>
<div class="math notranslate nohighlight">
\[p_{mk} = 1/ N_m \sum_{x_i \in R_m} I(y_i = k)\]</div>
<p>be the proportion of class k observations in node <span class="math notranslate nohighlight">\(m\)</span></p>
<p>Common measures of impurity are Gini</p>
<div class="math notranslate nohighlight">
\[H(X_m) = \sum_k p_{mk} (1 - p_{mk})\]</div>
<p>Entropy</p>
<div class="math notranslate nohighlight">
\[H(X_m) = - \sum_k p_{mk} \log(p_{mk})\]</div>
<p>and Misclassification</p>
<div class="math notranslate nohighlight">
\[H(X_m) = 1 - \max(p_{mk})\]</div>
<p>where <span class="math notranslate nohighlight">\(X_m\)</span> is the training data in node <span class="math notranslate nohighlight">\(m\)</span></p>
</div>
<div class="section" id="regression-criteria">
<h3>1.10.7.2. Regression criteria<a class="headerlink" href="#regression-criteria" title="Permalink to this headline">¶</a></h3>
<p>If the target is a continuous value, then for node <span class="math notranslate nohighlight">\(m\)</span>,
representing a region <span class="math notranslate nohighlight">\(R_m\)</span> with <span class="math notranslate nohighlight">\(N_m\)</span> observations, common
criteria to minimise as for determining locations for future
splits are Mean Squared Error, which minimizes the L2 error
using mean values at terminal nodes, and Mean Absolute Error, which
minimizes the L1 error using median values at terminal nodes.</p>
<p>Mean Squared Error:</p>
<div class="math notranslate nohighlight">
\[ \begin{align}\begin{aligned}\bar{y}_m = \frac{1}{N_m} \sum_{i \in N_m} y_i\\H(X_m) = \frac{1}{N_m} \sum_{i \in N_m} (y_i - \bar{y}_m)^2\end{aligned}\end{align} \]</div>
<p>Mean Absolute Error:</p>
<div class="math notranslate nohighlight">
\[ \begin{align}\begin{aligned}\bar{y}_m = \frac{1}{N_m} \sum_{i \in N_m} y_i\\H(X_m) = \frac{1}{N_m} \sum_{i \in N_m} |y_i - \bar{y}_m|\end{aligned}\end{align} \]</div>
<p>where <span class="math notranslate nohighlight">\(X_m\)</span> is the training data in node <span class="math notranslate nohighlight">\(m\)</span></p>
</div>
</div>
<div class="section" id="minimal-cost-complexity-pruning">
<span id="id1"></span><h2>1.10.8. Minimal Cost-Complexity Pruning<a class="headerlink" href="#minimal-cost-complexity-pruning" title="Permalink to this headline">¶</a></h2>
<p>Minimal cost-complexity pruning is an algorithm used to prune a tree to avoid
over-fitting, described in Chapter 3 of <a class="reference internal" href="#bre" id="id2"><span>[BRE]</span></a>. This algorithm is parameterized
by <span class="math notranslate nohighlight">\(\alpha\ge0\)</span> known as the complexity parameter. The complexity
parameter is used to define the cost-complexity measure, <span class="math notranslate nohighlight">\(R_\alpha(T)\)</span> of
a given tree <span class="math notranslate nohighlight">\(T\)</span>:</p>
<div class="math notranslate nohighlight">
\[R_\alpha(T) = R(T) + \alpha|T|\]</div>
<p>where <span class="math notranslate nohighlight">\(|T|\)</span> is the number of terminal nodes in <span class="math notranslate nohighlight">\(T\)</span> and <span class="math notranslate nohighlight">\(R(T)\)</span>
is traditionally defined as the total misclassification rate of the terminal
nodes. Alternatively, scikit-learn uses the total sample weighted impurity of
the terminal nodes for <span class="math notranslate nohighlight">\(R(T)\)</span>. As shown above, the impurity of a node
depends on the criterion. Minimal cost-complexity pruning finds the subtree of
<span class="math notranslate nohighlight">\(T\)</span> that minimizes <span class="math notranslate nohighlight">\(R_\alpha(T)\)</span>.</p>
<p>The cost complexity measure of a single node is
<span class="math notranslate nohighlight">\(R_\alpha(t)=R(t)+\alpha\)</span>. The branch, <span class="math notranslate nohighlight">\(T_t\)</span>, is defined to be a
tree where node <span class="math notranslate nohighlight">\(t\)</span> is its root. In general, the impurity of a node
is greater than the sum of impurities of its terminal nodes,
<span class="math notranslate nohighlight">\(R(T_t)&lt;R(t)\)</span>. However, the cost complexity measure of a node,
<span class="math notranslate nohighlight">\(t\)</span>, and its branch, <span class="math notranslate nohighlight">\(T_t\)</span>, can be equal depending on
<span class="math notranslate nohighlight">\(\alpha\)</span>. We define the effective <span class="math notranslate nohighlight">\(\alpha\)</span> of a node to be the
value where they are equal, <span class="math notranslate nohighlight">\(R_\alpha(T_t)=R_\alpha(t)\)</span> or
<span class="math notranslate nohighlight">\(\alpha_{eff}(t)=\frac{R(t)-R(T_t)}{|T|-1}\)</span>. A non-terminal node
with the smallest value of <span class="math notranslate nohighlight">\(\alpha_{eff}\)</span> is the weakest link and will
be pruned. This process stops when the pruned tree’s minimal
<span class="math notranslate nohighlight">\(\alpha_{eff}\)</span> is greater than the <code class="docutils literal notranslate"><span class="pre">ccp_alpha</span></code> parameter.</p>
<div class="topic">
<p class="topic-title">Examples:</p>
<ul class="simple">
<li><p><a class="reference internal" href="../auto_examples/tree/plot_cost_complexity_pruning.html#sphx-glr-auto-examples-tree-plot-cost-complexity-pruning-py"><span class="std std-ref">Post pruning decision trees with cost complexity pruning</span></a></p></li>
</ul>
</div>
<div class="topic">
<p class="topic-title">References:</p>
<dl class="citation">
<dt class="label" id="bre"><span class="brackets"><a class="fn-backref" href="#id2">BRE</a></span></dt>
<dd><p>L. Breiman, J. Friedman, R. Olshen, and C. Stone. Classification
and Regression Trees. Wadsworth, Belmont, CA, 1984.</p>
</dd>
</dl>
<ul class="simple">
<li><p><a class="reference external" href="https://en.wikipedia.org/wiki/Decision_tree_learning">https://en.wikipedia.org/wiki/Decision_tree_learning</a></p></li>
<li><p><a class="reference external" href="https://en.wikipedia.org/wiki/Predictive_analytics">https://en.wikipedia.org/wiki/Predictive_analytics</a></p></li>
<li><p>J.R. Quinlan. C4. 5: programs for machine learning. Morgan
Kaufmann, 1993.</p></li>
<li><p>T. Hastie, R. Tibshirani and J. Friedman. Elements of Statistical
Learning, Springer, 2009.</p></li>
</ul>
</div>
</div>
</div>


      </div>
    <div class="container">
      <footer class="sk-content-footer">
            &copy; 2007 - 2019, scikit-learn developers (BSD License).
          <a href="../_sources/modules/tree.rst.txt" rel="nofollow">Show this page source</a>
      </footer>
    </div>
  </div>
</div>
<script src="../_static/js/vendor/bootstrap.min.js"></script>

<script>
    window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
    ga('create', 'UA-22606712-2', 'auto');
    ga('set', 'anonymizeIp', true);
    ga('send', 'pageview');
</script>
<script async src='https://www.google-analytics.com/analytics.js'></script>


<script>
$(document).ready(function() {
    /* Add a [>>>] button on the top-right corner of code samples to hide
     * the >>> and ... prompts and the output and thus make the code
     * copyable. */
    var div = $('.highlight-python .highlight,' +
                '.highlight-python3 .highlight,' +
                '.highlight-pycon .highlight,' +
		'.highlight-default .highlight')
    var pre = div.find('pre');

    // get the styles from the current theme
    pre.parent().parent().css('position', 'relative');
    var hide_text = 'Hide prompts and outputs';
    var show_text = 'Show prompts and outputs';

    // create and add the button to all the code blocks that contain >>>
    div.each(function(index) {
        var jthis = $(this);
        if (jthis.find('.gp').length > 0) {
            var button = $('<span class="copybutton">&gt;&gt;&gt;</span>');
            button.attr('title', hide_text);
            button.data('hidden', 'false');
            jthis.prepend(button);
        }
        // tracebacks (.gt) contain bare text elements that need to be
        // wrapped in a span to work with .nextUntil() (see later)
        jthis.find('pre:has(.gt)').contents().filter(function() {
            return ((this.nodeType == 3) && (this.data.trim().length > 0));
        }).wrap('<span>');
    });

    // define the behavior of the button when it's clicked
    $('.copybutton').click(function(e){
        e.preventDefault();
        var button = $(this);
        if (button.data('hidden') === 'false') {
            // hide the code output
            button.parent().find('.go, .gp, .gt').hide();
            button.next('pre').find('.gt').nextUntil('.gp, .go').css('visibility', 'hidden');
            button.css('text-decoration', 'line-through');
            button.attr('title', show_text);
            button.data('hidden', 'true');
        } else {
            // show the code output
            button.parent().find('.go, .gp, .gt').show();
            button.next('pre').find('.gt').nextUntil('.gp, .go').css('visibility', 'visible');
            button.css('text-decoration', 'none');
            button.attr('title', hide_text);
            button.data('hidden', 'false');
        }
    });

	/*** Add permalink buttons next to glossary terms ***/
	$('dl.glossary > dt[id]').append(function() {
		return ('<a class="headerlink" href="#' +
			    this.getAttribute('id') +
			    '" title="Permalink to this term">¶</a>');
	});
  /*** Hide navbar when scrolling down ***/
  // Returns true when headerlink target matches hash in url
  (function() {
    hashTargetOnTop = function() {
        var hash = window.location.hash;
        if ( hash.length < 2 ) { return false; }

        var target = document.getElementById( hash.slice(1) );
        if ( target === null ) { return false; }

        var top = target.getBoundingClientRect().top;
        return (top < 2) && (top > -2);
    };

    // Hide navbar on load if hash target is on top
    var navBar = document.getElementById("navbar");
    var navBarToggler = document.getElementById("sk-navbar-toggler");
    var navBarHeightHidden = "-" + navBar.getBoundingClientRect().height + "px";
    var $window = $(window);

    hideNavBar = function() {
        navBar.style.top = navBarHeightHidden;
    };

    showNavBar = function() {
        navBar.style.top = "0";
    }

    if (hashTargetOnTop()) {
        hideNavBar()
    }

    var prevScrollpos = window.pageYOffset;
    hideOnScroll = function(lastScrollTop) {
        if (($window.width() < 768) && (navBarToggler.getAttribute("aria-expanded") === 'true')) {
            return;
        }
        if (lastScrollTop > 2 && (prevScrollpos <= lastScrollTop) || hashTargetOnTop()){
            hideNavBar()
        } else {
            showNavBar()
        }
        prevScrollpos = lastScrollTop;
    };

    /*** high preformance scroll event listener***/
    var raf = window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        window.oRequestAnimationFrame;
    var lastScrollTop = $window.scrollTop();

    if (raf) {
        loop();
    }

    function loop() {
        var scrollTop = $window.scrollTop();
        if (lastScrollTop === scrollTop) {
            raf(loop);
            return;
        } else {
            lastScrollTop = scrollTop;
            hideOnScroll(lastScrollTop);
            raf(loop);
        }
    }
  })();
});

</script>
    
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js"></script>
    
</body>
</html>